package layer;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import layer.MaskAdapter.MaskPanel;

public class MaskDemo extends JPanel {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setContentPane(new MaskDemo());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 600);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	int process = 0;

	public MaskDemo() {
		JPanel panel = new JPanel();
		for (int i = 1; i < 11; i++) {
			panel.add(new JButton("button" + i));
		}

		final MaskAdapter maskPanel = MaskAdapter.getMaskpanel(panel, false);

		final JCheckBox checkBox = new JCheckBox("mask");
		checkBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				maskPanel.setMask(checkBox.isSelected());
			}
		});
		checkBox.setSelected(false);

		final JComboBox comboBox = new JComboBox();
		comboBox.addItem("Alternation");
		comboBox.addItem("Fill");
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBox.getSelectedIndex() == 0) {
					maskPanel.getMask().setBlockStyle(MaskPanel.STYLE_ALTERNATION);
				} else {
					maskPanel.getMask().setBlockStyle(MaskPanel.STYLE_FILL);
				}
			}
		});

		final JTextField textField = new JTextField(8);
		textField.getDocument().addDocumentListener(new DocumentListener() {
			public void changedUpdate(DocumentEvent e) {
				maskPanel.getMask().setText(textField.getText());
			}

			public void insertUpdate(DocumentEvent e) {
				maskPanel.getMask().setText(textField.getText());
			}

			public void removeUpdate(DocumentEvent e) {
				maskPanel.getMask().setText(textField.getText());
			}

		});

		final JSlider slider = new JSlider(0, 1000);
		slider.setMinorTickSpacing(100);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				maskPanel.getMask().setTimeDelay(slider.getValue());
			}
		});
		slider.setValue(maskPanel.getMask().getTimeDelay());
		slider.setPaintTicks(true);

		final JCheckBox modelCheckBox = new JCheckBox("Process");

		final Timer timer = new Timer(500, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				maskPanel.getMask().setProcessValue(process++);//
				if (process == 100) {
					process = 0;
				}
			}
		});
		modelCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (modelCheckBox.isSelected()) {
					timer.start();
				} else {
					maskPanel.getMask().setProcessValue(-Double.MAX_VALUE);
					process = 0;
					timer.stop();
				}
			}
		});
		JPanel controlPanel = new JPanel();
		controlPanel.add(checkBox);
		controlPanel.add(comboBox);
		controlPanel.add(textField);
		controlPanel.add(slider);
		controlPanel.add(modelCheckBox);

		this.setLayout(new BorderLayout());
		this.add(maskPanel, BorderLayout.CENTER);
		this.add(controlPanel, BorderLayout.NORTH);
	}
}
